<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Support extends MY_Controller {

    private $_emailConfig;

    public function __construct() {
        parent::__construct();
        $this->load->model('support_mod');
        $this->load->model('users_mod');
        $this->load->library('email');

        // define terms for emails
        $this->_emailConfig['mailtype'] = 'html';
        $this->_emailConfig['useragent'] = 'PHP CodeIgniter';
    }

    public function index() {
        $data['menu_page'] = "support"; // Koja je stranica. current menu item
        $data['users'] = $this->support_mod->getUsers();
        $data['messages'] = NULL;
        $data['contents'] = 'support/support_home';
        $this->load->view('main', $data);
    }
    public function refresh_users(){
        try {
        $data['users'] = $this->support_mod->getUsers();
        $html = $this->load->view('support/users_list', $data, true);
            $response = array(
                'ind' => 1,
                'msg' => "",
                'html' => $html
            );
        } catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'html' => ''
            );
        }
        echo json_encode($response);
    }

    public function show_support_messages() {
        try {
            $userId = $this->input->post('userId');

            if ($userId == NULL || $userId=="")
                $data['messages'] = NULL;
            else
                $data['messages'] = $this->support_mod->getDialog($userId);

            $html = $this->load->view('support/dialog_list', $data, true);
            $response = array(
                'ind' => 1,
                'msg' => "",
                'html' => $html
            );
        } catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'html' => ''
            );
        }
        echo json_encode($response);
    }
    public function submit_support_message(){
        try {
            $userId = $this->input->post('userId');
            $message=$this->input->post('message');
            if($userId!=null&&$userId!="")
            $this->support_mod->insertMessage($userId,$message);
            $data['messages'] = $this->support_mod->getDialog($userId);
            $html = $this->load->view('support/dialog_list', $data, true);
            $response = array(
                'ind' => 1,
                'msg' => "",
                'html' => $html
            );
        } catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'html' => ''
            );
        }
        echo json_encode($response);
    }

}

?>
